package com.baymax.test.scene.span

import android.graphics.Paint
import android.text.TextPaint
import android.text.style.AbsoluteSizeSpan
import android.text.style.LineHeightSpan
import kotlin.math.max

class LineStyleSpan(size: Float, val height: Float, val maxLine: Int = 1, val color: Int) :
    AbsoluteSizeSpan(size.toInt()), LineHeightSpan {

    override fun updateDrawState(ds: TextPaint) {
        super.updateDrawState(ds)
        ds.color = color
    }

    override fun updateMeasureState(p: TextPaint) {
        super.updateMeasureState(p)
        p.color = color
    }

    var index = 1

    override fun chooseHeight(
        text: CharSequence?,
        start: Int,
        end: Int,
        spanstartv: Int,
        lineHeight: Int,
        fm: Paint.FontMetricsInt
    ) {
        val originHeight = fm.descent - fm.ascent
        // If original height is not positive, do nothing.
        // If original height is not positive, do nothing.
        if (originHeight <= 0) {
            return
        }
        if (spanstartv == lineHeight) {
            val ratio: Float = height / originHeight
            fm.descent = Math.round(fm.descent * ratio)
            fm.ascent = (fm.descent - height).toInt()
        } else {
            index++
            if (index > maxLine) {

            }

        }
    }

    //    override fun getSize(
//        paint: Paint,
//        text: CharSequence,
//        start: Int,
//        end: Int,
//        fm: Paint.FontMetricsInt?
//    ): Int {
//        paint.textSize = size
//        return paint.measureText(text, start, end).toInt()
//    }
//
//    override fun draw(
//        canvas: Canvas,
//        text: CharSequence,
//        start: Int,
//        end: Int,
//        x: Float,
//        top: Int,
//        y: Int,
//        bottom: Int,
//        paint: Paint
//    ) {
//        paint.textSize = size
//        paint.color = color
//        val pW = canvas.width
//        val pH = canvas.height
//        val tW = paint.measureText(text, start, end).toInt()
//        canvas.drawText(text, start, end, x.toFloat(), y + size, paint)
//    }
}